using System;
using Microsoft.SPOT;
using System.Xml;
using System.Ext.Xml;

namespace mfXmlSerializer
{
    /// <summary>
    /// static container class 4 all xml extension methods
    /// </summary>
    public static class mfXmlExtensions
    {
        public static void AssertIfMapIsInvalid(this XmlReader xmlReader, mfSerializationMap map)
        {
            if (xmlReader.Name != map.TargetType.Name && xmlReader.NodeType != XmlNodeType.Element)
            {
                throw new mfXmlException("no '" + map.TargetType.Name + "' element found");
            }
        }

        public static bool IsElementInvalid(this XmlReader xmlReader, mfMapBaseElement mapElement)
        {
            if (xmlReader.Name != mapElement.Name || xmlReader.NodeType != XmlNodeType.Element)
            {
                return true;
            }
            return false;
        }

        public static bool IsNotNodeTextType(this XmlReader xmlReader, mfMapBaseElement mapElement)
        {
            if (xmlReader.NodeType != XmlNodeType.Text)
            {
                return true;
            }
            return false;
        }

        public static void AssertIfEndElementIsInvalid(this XmlReader xmlReader, mfMapBaseElement mapElement)
        {
            if (xmlReader.Name != mapElement.Name && xmlReader.NodeType != XmlNodeType.EndElement)
                throw new mfXmlException("no '" + mapElement.Name + "' end-element found");
        }

        public static void WriteElementStringIfNotNull(this XmlWriter xmlWriter, string localName, string value)
        {
            if (value == null)
            {
                return;
            }
            if (localName.IsNullOrEmpty())
            {
                throw new mfXmlException("WriteElement localName is null or empty");
            }

            xmlWriter.WriteElementString(localName, value);
        }
    }
}
